package test.shadow;

import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattDescriptor;
import android.bluetooth.BluetoothGattService;

import java.util.Arrays;
import java.util.List;

import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.annotation.Resetter;
import org.robolectric.shadows.ShadowBluetoothGatt;

import test.util.Log;

import static android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
import static android.os.Build.VERSION_CODES.LOLLIPOP;

@Implements(value = BluetoothGatt.class, minSdk = JELLY_BEAN_MR2)
public class MyShadowBluetoothGatt extends ShadowBluetoothGatt {
    private static final String TAG = MyShadowBluetoothGatt.class.getSimpleName();

    private static ShadowUtil.MethodListener methodListener;
    private static BluetoothGattService[] services;

    @Resetter
    public static void resetMe() {
        Log.d(TAG, "resetMe");
        methodListener = null;
        services = null;
    }

    public static void setMethodListener(ShadowUtil.MethodListener listener) {
        methodListener = listener;
    }

    public static void setServices(BluetoothGattService[] services_) {
        services = services_;
    }

    @Implementation(minSdk = JELLY_BEAN_MR2)
    public boolean discoverServices() {
        if (methodListener != null) {
            String name = Thread.currentThread().getStackTrace()[1].getMethodName();
            Object[] parameters = null;
            Object ret = methodListener.onCallMethod(name, parameters);
            return ((boolean)ret);
        }

        return false;
    }

    @Implementation(minSdk = JELLY_BEAN_MR2)
    public List<BluetoothGattService> getServices() {
        return (services != null ? Arrays.asList(services) : null);
    }

    @Implementation(minSdk = JELLY_BEAN_MR2)
    public boolean setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enable) {
        if (methodListener != null) {
            String name = Thread.currentThread().getStackTrace()[1].getMethodName();
            Object[] parameters = new Object[] { characteristic, enable };
            Object ret = methodListener.onCallMethod(name, parameters);
            return ((boolean)ret);
        }

        return false;
    }

    @Implementation(minSdk = JELLY_BEAN_MR2)
    public boolean writeDescriptor(BluetoothGattDescriptor descriptor) {
        if (methodListener != null) {
            String name = Thread.currentThread().getStackTrace()[1].getMethodName();
            Object[] parameters = new Object[] { descriptor };
            Object ret = methodListener.onCallMethod(name, parameters);
            return ((boolean)ret);
        }

        return false;
    }

    @Implementation(minSdk = LOLLIPOP)
    public boolean requestMtu(int mtu) {
        if (methodListener != null) {
            String name = Thread.currentThread().getStackTrace()[1].getMethodName();
            Object[] parameters = new Object[] { mtu };
            Object ret = methodListener.onCallMethod(name, parameters);
            return ((boolean)ret);
        }

        return false;
    }

    @Implementation(minSdk = JELLY_BEAN_MR2)
    public boolean writeCharacteristic(BluetoothGattCharacteristic characteristic) {
        if (methodListener != null) {
            String name = Thread.currentThread().getStackTrace()[1].getMethodName();
            Object[] parameters = new Object[] { characteristic };
            Object ret = methodListener.onCallMethod(name, parameters);
            return ((boolean)ret);
        }

        return false;
    }

    @Implementation(minSdk = JELLY_BEAN_MR2)
    public void disconnect() {
        if (methodListener != null) {
            String name = Thread.currentThread().getStackTrace()[1].getMethodName();
            Object[] parameters = null;
            methodListener.onCallMethod(name, parameters);
        }
    }
}
